using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using UnityEngine;
using Quaternion = UnityEngine.Quaternion;
using Vector2 = UnityEngine.Vector2;
using Vector3 = UnityEngine.Vector3;

namespace Enemy
{
    public class FSM_Enemy2 : FSM
    {

        protected override void Awake()
        {
            base.Awake();
            
            states.Add(StateType.Idle, new IdleState(this));
            states.Add(StateType.Attack1, new Attack1State(this));
            // states.Add(StateType.Hit,new HitState(this));
            // states.Add(StateType.Death,new DeathState(this));

            TransitionState(StateType.Idle);
        }

        void Start()
        {

            tag = "SE";

            parameter.rbody = GetComponent<Rigidbody2D>();

            parameter.anim = GetComponent<Animator>();

        }

        // Update is called once per frame
        void Update()
        {
            //if (!pc.alive)
            //{
            //    parameter.target.gameObject.layer = LayerMask.GetMask("DieLayer");
            //    parameter.target = null;
            //}


            // if (parameter.health == 0)
            // {
            //     TransitionState(StateType.Death);
            // }

            currentState.OnUpdate();
        }

        public override void FlipTo(Transform target)
        {
            if (target)
            {
                if (transform.position.x > target.position.x)
                {
                    transform.localScale = new Vector3(-5, 5, 1);
                }
                else if (transform.position.x < target.position.x)
                {
                    transform.localScale = new Vector3(5, 5, 1);
                }
            }
        }


        // public void OnCollisionEnter2D(Collision2D other)
        // {
        //     if (other.collider.CompareTag("PlayerAttack1"))
        //     {
        //         parameter.hurttag = "Attack1";
        //         TransitionState(StateType.Hit);
        //     }
        //
        //     if (other.collider.CompareTag("PlayerAttack2"))
        //     {
        //         parameter.hurttag = "Attack2";
        //         TransitionState(StateType.Hit);
        //     }
        // }

        public void Attack()
        {
            Vector3 position = parameter.rbody.position + new Vector2(2 * transform.localScale.x / 7, 1);
            Vector3 dir = parameter.target.position - position;
            dir.z = 0;
            Quaternion rotation = Quaternion.FromToRotation(Vector3.right, dir);
            GameObject bullet = Instantiate(parameter.Bullet, position, rotation);
            BulletController_Enemy bc = bullet.GetComponent<BulletController_Enemy>();
            if (bc != null)
            {
                bc.Move(
                    new Vector2(parameter.target.position.x - position.x, parameter.target.position.y + 1 - position.y)
                        .normalized, 200);
            }
        }

        //敌人的吹风技能
        public void wind()
        {
            GameObject wind = Instantiate(parameter.wind_prefab, parameter.target.position, Quaternion.identity);
        }

        public void gravity(int grade)
        {
            GameObject gravity = Instantiate(parameter.gravity_prefab, parameter.target.position, Quaternion.identity);
            gravity.transform.SetParent(parameter.target);
            if (grade == 1)
            {
                Gravity.Instance.Enhance();
                Debug.Log(Gravity.Instance.rigidbodies.First().gravityScale);
            }
            else if (grade == -1)
            {
                Gravity.Instance.Enhance();
                Debug.Log(Gravity.Instance.rigidbodies.First().gravityScale);
            }
        }

        public void fog(int grade)
        {
            GameObject fog = GameObject.Find("Fog");
            fog.SetActive(true);
            Fog fs = fog.GetComponent<Fog>();
            fs.changeFog(grade);
        }

        public void OnDrawGizmos()
        {
            Gizmos.DrawWireSphere(parameter.attackPoint.position, parameter.attackArea);
        }

        protected override void Gain()
        {
            Wind.Instance.Destroy();
            PlayerController.Instance.isTrapped = false;
            PlayerController.Instance.rb.simulated = true;
            PlayerController.Instance.flag = 0;
            PlayerController.Instance.rb.AddForce(new Vector2(0,30), ForceMode2D.Impulse);
        }

        protected override void Loss()
        {
            Wind.Instance.Destroy();
            PlayerController.Instance.isTrapped = false;
            PlayerController.Instance.rb.simulated = true;
            PlayerController.Instance.flag = 0;
        }
    }
}
